import AluGeoUtil from '@/model/geometry/utils/AluGeoUtil';
import { Path, Vector2 } from 'three';
import { CreateMeta, HoleMeta } from '@/model/meta/MetaClass';
import PolygonUtil from '@/model/utils/Polygon';
import { StringUtil } from '@/model/utils/StringUtil';
import { polygon } from '@turf/helpers';
import difference from '@turf/difference';


/**
 * 普通封板
 */
export default class SPEntity {

    //模型构件
    static __create(data: any): GeometryReturnMeta<Array<any>> {

        data[""]

        let bb = this.__basicBoard(data);

        this.setDuan(data);

        return {
            vertices: bb.vertices,
            indices: bb.indices,
            meshface: []
        }
    }

    private static setDuan(data: any) {
        data["duan"] = [
            0, 0, 0,
            data.W, 0, 0,
            data.W, data.L, 0,
            0, data.L, 0,
        ];
        data["zhong"] = {
            L: [
                {
                    pointer: [data.W / 2, data.L, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
            ],
            RL: [
                {
                    pointer: [data.W / 2, 0, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
            ],
            W: [
                {
                    pointer: [data.W, data.L / 2, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
            ],
            RW: [
                {
                    pointer: [0, data.L / 2, 0],
                    center: [data.W / 2, data.L / 2, 0]
                },
            ]
        }
    }

    /** 获得一系列的洞点数据*/
    private static __createHoleMeta(data: any, extra): HoleMeta {

        /** 直径1*/
        // let holeStr = data.p_r_pos01;
        // let hole0 = StringUtil.formStringToArray(holeStr);
        let hole0 = this.filterHoles(data, "p_r_pos01", (+data.p_r_d1) * 0.5);


        /** 直径2*/
        // let holeStr1 = data.p_r_pos2;
        // let hole1 = StringUtil.formStringToArray(holeStr1);
        let hole1 = this.filterHoles(data, "p_r_pos2", (+data.p_r_d2) * 0.5);


        /** 直径3*/
        // let holeSt2 = data.p_r_pos3;
        // let hole2 = StringUtil.formStringToArray(holeSt2);
        let hole2 = this.filterHoles(data, "p_r_pos3", (+data.p_r_d3) * 0.5);



        let hole = [hole0, hole1, hole2];
        let r = [(+data.p_r_d1) * 0.5, (+data.p_r_d2) * 0.5, (+data.p_r_d3) * 0.5];
        let path = new Path();
        let rs = [];

        for (let i: number = 0; i < hole.length; i++) {
            let h = hole[i];
            let rr = r[i];
            for (let j: number = 0; j < h.length; j += 2) {
                let p = path.clone();
                let x = +h[j];
                let y = +h[j + 1];
                p.moveTo(x, y).absarc(x, y, rr, 0.0, 2 * Math.PI, false);
                rs.push(p);
            }
        }




        return new HoleMeta(0, 0, [], "y", rs, extra);
    }


    private static filterHoles(data: any, key: string, d: number) {
        let str = data[key];

        let zqkwd = 0;
        let zqkgd = 0;
        let zqkwoff = 0;
        let zqkloff = 0;
        let yqkwd = 0;
        let yqkgd = 0;
        let yqkwoff = 0;
        let yqkloff = 0;

        let w = +data.W

        let rect = [
            new Vector2(zqkwoff, zqkloff),
            new Vector2(zqkwoff + zqkwd, zqkloff),
            new Vector2(zqkwoff + zqkwd, zqkloff + zqkgd),
            new Vector2(zqkwoff, zqkloff + zqkgd),

        ];


        let start = w - yqkwoff - yqkwd;
        let r1 = [
            new Vector2(start, yqkloff),
            new Vector2(start + yqkwd, yqkloff),
            new Vector2(start + yqkwd, yqkloff + yqkgd),
            new Vector2(start, yqkloff + yqkgd),
        ];

        if (!!str) {
            let r = []
            let hole = StringUtil.formStringToArray(str);

            for (let i: number = 0; i < hole.length; i += 2) {
                let x = hole[i];
                let y = hole[i + 1];

                let rr = [
                    new Vector2(x - d, y - d),
                    new Vector2(x + d, y - d),
                    new Vector2(x + d, y + d),
                    new Vector2(x - d, y + d),
                ]

                let isInter = PolygonUtil.intersectRect(rr, rect) ? true : PolygonUtil.intersectRect(rr, r1);
                if (!isInter) {
                    r.push(x, y);
                }
            }

            return r;
        }

        return [];
    }


    /** 创建一个基准面*/
    private static __basicBoard(data: any): GeometryReturnMeta<Array<any>> {

        let l = +data.L, w = +data.W, t = +data.bkT;

        let r = this.buildBasicPlanePath(data);
        let back = r[0][0];
        let front = r[0][1];

        let h = this.__createHoleMeta(data, null);
        let c: CreateMeta = new CreateMeta(t, front, back, h);

        return AluGeoUtil.createBoardWithHole(c);
    }


    static buildBasicPlanePath(data: any): Array<Array<number>> {

        //缺口参数
        let zqkwd = 0;
        let zqkgd = 0;
        let zqkwoff = 0;
        let zqkloff = 0;
        let yqkwd = 0;
        let yqkgd = 0;
        let yqkwoff = 0;
        let yqkloff = 0;

        let l = +data.L, w = +data.W, t = +data.bkT;

        /** 这里是用框架的  */
        let f = polygon([[
            [0, 0],
            [w, 0],
            [w, l],
            [0, l],
            [0, 0]
        ]])

        //左缺口的参数
        let p1 = polygon([
            [
                [zqkwoff, zqkloff],
                [zqkwoff + zqkwd, zqkloff],
                [zqkwoff + zqkwd, zqkloff + zqkgd],
                [zqkwoff, zqkloff + zqkgd],
                [zqkwoff, zqkloff],
            ]
        ])

        //右缺口参数
        let start = w - yqkwoff;
        let p2 = polygon([
            [
                [start, yqkloff],
                [start, yqkloff + yqkgd],
                [start - yqkwd, yqkloff + yqkgd],
                [start - yqkwd, yqkloff],
                [start, yqkloff],
            ]
        ])

        let r = difference(f, p1);
        let rr = difference(r.geometry, p2);

        let face = [];
        let hole = [];
        let g = rr.geometry.coordinates;

        for (let i: number = 0; i < g.length; i++) {
            let b = [];
            let ff = [];

            let gg = g[i];
            for (let j: number = 0; j < gg.length - 1; j++) {
                b.push(...gg[j], 0);
                ff.push(...gg[j], t);
            }


            if (i == 0) {
                face.push(b, ff);
            }
            else {
                hole.push([b, ff]);
            }
        }

        return [face, hole];



        /** 这里自己写的， 用框架就是舒服的很*/


        // //不做缺口
        // if (zqkwoff + zqkwd + yqkwd + yqkloff >= w) {
        //     let back = [
        //         0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        //     ];

        //     let front = [
        //         0, 0, t, w, 0, t, w, l, t, 0, l, t,
        //     ];

        //     return [back, front]
        // }

        // //存在共边
        // if (zqkloff == 0 || zqkwoff == 0 || yqkloff == 0 || yqkwoff == 0) {
        //     //存在共边--l
        //     if (zqkloff == 0 && zqkwoff == 0) {
        //         //直接改第一个节点即可
        //     }
        //     else if (zqkloff == 0) {
        //         //在最后一个和第一个节点之间构建
        //     }
        //     else if (zqkwoff == 0) {
        //         //在第一个和第二个节点之间构建
        //     }
        //     else {
        //         //直接挖洞
        //     }


        //     //存在共边--r
        //     if (yqkloff == 0 && yqkwoff == 0) {

        //     }
        //     else if (yqkloff == 0) {

        //     }
        //     else if (yqkwoff == 0) {

        //     }
        //     else {
        //         //直接挖洞
        //     }
        // }
        // else {
        //     //挖两个洞
        //     let hole = [];
        //     let hole1 = [];
        // }


        //参数处理 左右切的点
        //  let fl = [0, 0, 0];
        //  if (lcl > 0 && lcw > 0) {
        //      fl = [];
        //      fl.push(0, lcl, 0);
        //      fl.push(lcw, lcl, 0);
        //      fl.push(lcw, 0, 0);
        //  }

        //  let f2 = [w, 0, 0];
        //  if (rcl > 0 && rcw > 0) {
        //      f2 = [];
        //      f2.push(w - rcw, 0, 0);
        //      f2.push(w - rcw, rcl, 0);
        //      f2.push(w, rcl, 0);
        //  }

        // if(zqkw)

        // let r1 = [
        //     new Vector2(zqkwoff, zqkloff),
        //     new Vector2(zqkwoff + zqkwd, zqkloff),
        //     new Vector2(zqkwoff + zqkwd, zqkloff + zqkgd),
        //     new Vector2(zqkwoff + zqkwd, zqkloff),

        // ]
    }
}